package com.neuedu.boot.controller;

import com.neuedu.boot.entity.User;
import com.neuedu.boot.framework.Res;
import org.eclipse.jdt.internal.compiler.ast.ArrayReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 项目：      springboot
 * 类名:       IndexController
 * 创建时间：  2024/3/26 09：23
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@RestController
public class IndexController {


    //从容器中获取使用@Bean声明的对象
    @Autowired
    @Qualifier("appName")
    String appName;


    @RequestMapping("appName")
    String appName(){
        return  appName;
    }




    /**
     * http://127.0.0.1:8080/index
     * @return
     */
    @RequestMapping("/index")
    String index(){
        return "success";
    }


    /**
     * http://127.0.0.1:8080/map
     * @return
     */
    @RequestMapping("/map")
    Map map(){
        Map result = new HashMap();
        result.put("success",true);
        result.put("code",200);//200 404 500 302
        result.put("msg","主键重复.....");

        return  result;
    }



    /**
     * http://127.0.0.1:8080/getById
     * @return
     */
    @RequestMapping("/getById")
    User getById( ){
      //模拟从数据库查询一条结果 实体类 User

        User user =  new User(1,"张三","男","123456789123456789",new Date(),1);
        return  user;
    }


    /**
     * http://127.0.0.1:8080/getById
     * @return
     */
    @RequestMapping("/pageList")
    List<User> pageList( ){
        //模拟从数据库查询一条结果 实体类 User
        List list = new ArrayList();

        for (int i = 0; i < 10; i++) {
            User user =  new User(1+i,"张三"+i,"男"+i,"123456789123456789"+i,new Date(),1);
            list.add(user);
        }



        return  list;
    }


    /**
     * http://127.0.0.1:8080/resPageList
     * @return
     */
    @RequestMapping("/resPageList")
    Res<List<User>> resPageList( ){
        //模拟从数据库查询一条结果 实体类 User
        List list = new ArrayList();

        for (int i = 0; i < 10; i++) {
            User user =  new User(1+i,"张三"+i,"男"+i,"123456789123456789"+i,new Date(),1);
            list.add(user);
        }

        System.out.println("list = " + list);

        return  Res.ok(list);
    }



    @RequestMapping("/resGetById")
    Res<User> resGetById( ){
        //模拟从数据库查询一条结果 实体类 User

        User user =  new User(1,"张三","男","123456789123456789",new Date(),1);
        return  Res.ok(user);
    }



    @RequestMapping("/my/error")
    Res<Object> resError(){
        //模拟从数据库查询一条结果 实体类 User
        //失败了，应该 返回  500\
        // try {
        //     //调用service
        // }catch (Exception e ){
        //     e.getMessage()
        // }

        // return  Res.failed(404,"xxxx失败了");
        return  Res.failed("xxxx失败了");
    }



}
